home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1999 March / EnigmA AMIGA RUN 35 (1999)(G.R. Edizioni)(IT)[!][issue 1999-03].iso / earcd / devel / gencodec / source / gencodec.c next >
C/C++ Source or Header  |  1999-01-01  |  8KB  |  306 lines

  1. #include "Tools.h"
  2. #include "WriteCatalogFiles.h"
  3. #include "WriteExternalFile.h"
  4. #include "WriteGUIFiles.h"
  5. #include "WriteMainFile.h"
  6. #include "GenCodeCGUI.h"
  7. #include <libraries/mui.h>
  8.  
  9. #include <ctype.h>
  10.  
  11. /* Prototypes */
  12. #ifdef __GNUC__
  13. #include <proto/alib.h>
  14. #include <proto/exec.h>
  15. #include <proto/dos.h>
  16. #else
  17. #include <clib/alib_protos.h>
  18. #include <clib/exec_protos.h>
  19. #include <clib/dos_protos.h>
  20. #endif /* __GNUC__ */
  21.  
  22. #include <exec/memory.h>
  23.  
  24. /* ANSI */
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <stdlib.h>
  28.  
  29. /* Pragmas */
  30. #include <pragmas/dos_pragmas.h>
  31.  
  32. /* MUIBuilder library */
  33. #include "MB.h"
  34. #include "MB_pragmas.h"
  35. #include "MB_protos.h"
  36.  
  37. #ifdef __SASC
  38. extern struct Library * DOSBase;
  39. #endif /* __SASC */
  40.  
  41. /****************************************************************************************************************/
  42. /*****                                                                                                        *****/
  43. /**                                                Global variables                                                **/
  44. /*****                                                                                                        *****/
  45. /****************************************************************************************************************/
  46.  
  47. ULONG    varnb;                    /* number of variables */
  48.  
  49. BOOL    Code, Env;                /* flags-options */
  50. BOOL    Locale, Declarations;
  51. BOOL    Notifications, Application;
  52. BOOL    ExternalExist = FALSE;
  53. char    *FileName, *CatalogName;/* Strings */
  54. char    *GetString;
  55. char    *GetMBString;
  56.  
  57. char    *HeaderFile;
  58. char    *GUIFile;
  59. char    *MBDir;
  60. char    *Externals;
  61. char    *MainFile;
  62. char    *Catalog_h_File;
  63. char    *Catalog_c_File;
  64.  
  65. extern void end(void *);
  66.  
  67. void Quit(void *);
  68.  
  69. /****************************************************************************************************************/
  70. /*****                                                                                                        *****/
  71. /**                                                 Init                                                           **/
  72. /*****                                                                                                        *****/
  73. /****************************************************************************************************************/
  74.  
  75. BOOL Init(void)
  76. {
  77.     HeaderFile         = NULL;
  78.     GUIFile         = NULL;
  79.     MBDir            = NULL;
  80.     Externals         = NULL;
  81.     MainFile        = NULL;
  82.     Catalog_h_File    = NULL;
  83.     Catalog_c_File    = NULL;
  84.  
  85.     /* Get all needed variables */
  86.     MB_Get    (
  87.             MUIB_VarNumber        , &varnb,
  88.             MUIB_Code            , &Code,
  89.             MUIB_Environment    , &Env,
  90.             MUIB_Locale            , &Locale,
  91.             MUIB_Notifications    , &Notifications,
  92.             MUIB_Declarations    , &Declarations,
  93.             MUIB_Application    , &Application,
  94.             MUIB_FileName        , &FileName,
  95.             MUIB_CatalogName    , &CatalogName,
  96.             MUIB_GetStringName    , &GetString,
  97.             TAG_END
  98.         );
  99.  
  100.     /* Verify some varaiables*/
  101.     if (*FileName=='\0')
  102.     {
  103.         DisplayMsg("Please give a name to your application in the field \"CODE\"\n");
  104.         return FALSE;
  105.     }
  106.     if (Locale && *CatalogName=='\0')
  107.     {
  108.         DisplayMsg("Please give a catalog name in the field \"CATALOG\"\n");
  109.         return FALSE;
  110.     }
  111.     if (Locale && *GetString=='\0')
  112.     {
  113.         DisplayMsg("Please give a \"GetString\" name in the field \"GetString\"\n");
  114.         return FALSE;
  115.     }
  116.  
  117.     /* Create 'GetMBString' name */
  118.     if (strcmp(GetString, "GetMBString") == 0) 
  119.         GetMBString  = "GetMBString2";
  120.     else
  121.         GetMBString = "GetMBString";
  122.  
  123.     /* Create File Names */
  124.     remove_extend(FileName);
  125.  
  126.     GUIFile = AllocMemory(strlen(FileName)+6,TRUE);
  127.     strcpy(GUIFile, FileName);
  128.     add_extend(GUIFile, "GUI.c");
  129.  
  130.     HeaderFile = AllocMemory(strlen(FileName)+6,TRUE);
  131.     strcpy(HeaderFile, FileName);
  132.     add_extend(HeaderFile, "GUI.h");
  133.  
  134.     Externals = AllocMemory(strlen(FileName)+9,TRUE);
  135.     strcpy(Externals, FileName);
  136.     strcat(Externals, "Extern");
  137.     add_extend(Externals, ".h");
  138.  
  139.     MainFile = AllocMemory(strlen(FileName)+7,TRUE);
  140.     strcpy(MainFile, FileName);
  141.     strcat(MainFile, "Main");
  142.     add_extend(MainFile, ".c");
  143.  
  144.     Catalog_h_File = AllocMemory(strlen(FileName)+7,TRUE);
  145.     strcpy(Catalog_h_File, FileName);
  146.     strcat(Catalog_h_File, "_cat");
  147.     add_extend(Catalog_h_File, ".h");
  148.  
  149.     Catalog_c_File = AllocMemory(strlen(FileName)+7,TRUE);
  150.     strcpy(Catalog_c_File, FileName);
  151.     strcat(Catalog_c_File, "_cat");
  152.     add_extend(Catalog_c_File, ".c");
  153.  
  154.     /* Get Current Directory Name */
  155.     MBDir = GetCurrentDirectory();
  156.  
  157.     return TRUE;
  158. }
  159.  
  160.  
  161. /****************************************************************************************************************/
  162. /*****                                                                                                        *****/
  163. /**                                             FreeFileNameMemory                                                   **/
  164. /*****                                                                                                        *****/
  165. /****************************************************************************************************************/
  166.  
  167. void FreeFileNameMemory(void)
  168. {
  169.     FreeMemory(HeaderFile);
  170.     FreeMemory(GUIFile);
  171.     FreeMemory(Externals);
  172.     FreeMemory(MainFile);
  173.     FreeMemory(MBDir);
  174.     FreeMemory(Catalog_h_File);
  175.     FreeMemory(Catalog_c_File);
  176. }
  177.  
  178. /****************************************************************************************************************/
  179. /*****                                                                                                        *****/
  180. /**                                                 Quit                                                           **/
  181. /*****                                                                                                        *****/
  182. /****************************************************************************************************************/
  183.  
  184. void Quit(void *App)
  185. {
  186.     FreeFileNameMemory();
  187.     end(App);
  188. }
  189.  
  190. /****************************************************************************************************************/
  191. /*****                                                                                                        *****/
  192. /**                                            PrintInfo                                                           **/
  193. /*****                                                                                                        *****/
  194. /****************************************************************************************************************/
  195. void PrintInfo(struct ObjApp *App,char *str)
  196. {
  197.     char *msg;
  198.  
  199.     if ((msg = (char *)AllocMemory(9+strlen(str)+4+1,FALSE)))
  200.     {
  201.         sprintf(msg,"Generate %s ...",str);
  202.         set(App->TX_Prg_Name,MUIA_Text_Contents,msg);
  203.         FreeMemory(msg);
  204.     }
  205. }
  206.  
  207. /****************************************************************************************************************/
  208. /*****                                                                                                        *****/
  209. /**                                             GenerateCode                                                       **/
  210. /*****                                                                                                        *****/
  211. /****************************************************************************************************************/
  212.  
  213. void GenerateCode(struct ObjApp *App,char *H_Header_Text,char *C_Header_Text,char *Main_Header_Text)
  214. {
  215.     ULONG                Main;
  216.     BPTR                lock;
  217.     TypeQuitFunction    quit_f;
  218.     void                *data;
  219.  
  220.     data   = SetDataQuit((void *)App);
  221.     quit_f = SetFunctionQuit(Quit);
  222.  
  223.     if (!Init())
  224.         Quit((void *)App);
  225.  
  226.     /* test catalog description file if locale */
  227.     if (Locale)
  228.     {
  229.         if (!(lock=Lock(CatalogName,ACCESS_READ)))
  230.         {
  231.             DisplayMsg("The catalog description file doesn't exist !!\nPlease generate it with MUIBuilder");
  232.             Quit((void *)App);
  233.         }
  234.         UnLock(lock);
  235.     }
  236.  
  237.     if (Declarations)
  238.     {
  239.         PrintInfo(App,FilePart(HeaderFile));
  240.         WriteHeaderFile(HeaderFile,H_Header_Text,FileName,varnb,Env,Notifications,Locale);
  241.     }
  242.  
  243.     if (Env)
  244.     {
  245.         PrintInfo(App,FilePart(Externals));
  246.         ExternalExist = WriteExternalFile(Externals,varnb);
  247.     }
  248.  
  249.     PrintInfo(App,FilePart(GUIFile));
  250.     WriteGUIFile(MBDir,HeaderFile,GUIFile,C_Header_Text,Externals,GetString,GetMBString,varnb,
  251.                  ExternalExist,Env,Locale,Declarations,Code,Notifications);
  252.  
  253.     get(App->CH_Generate_Main_File,MUIA_Selected,&Main);
  254.     if (Main)
  255.     {
  256.         PrintInfo(App,FilePart(MainFile));
  257.         WriteMainFile(HeaderFile,MainFile,Main_Header_Text,varnb,Locale);
  258.     }
  259.  
  260.     if (Locale)
  261.     {
  262.         ULONG    Catalog;
  263.  
  264.         get(App->CH_Add_new_entries_in_Catalog_Description_File,
  265.             MUIA_Selected,&Catalog);
  266.         if (Catalog)
  267.         {
  268.             char    *command;
  269.  
  270.             command=AllocMemory(strlen(MBDir)+1+25+15+strlen(CatalogName)+6+strlen(GetString)+1,TRUE);
  271.  
  272.             strcpy(command,MBDir);
  273.             AddPart(command,"WriteCatalog/WriteCatalog",strlen(MBDir)+1+25+1);
  274.             if (lock = Lock(command, ACCESS_READ))
  275.             {
  276.                 UnLock(lock);
  277.                 strcat(command," Reserved CDN \"");
  278.                 strcat(command,CatalogName);
  279.                 strcat(command,"\" GSN ");
  280.                 strcat(command,GetString);
  281.                 if (!Execute(command,NULL,NULL))
  282.                     DisplayMsg("Can't launch WriteCatalog !!!");
  283.             }
  284.             else
  285.                 DisplayMsg("Can't find WriteCatalog !!!");
  286.  
  287.             FreeMemory(command);
  288.         }
  289.  
  290.         PrintInfo(App,FilePart(Catalog_h_File));
  291.         if (!Write_Catalog_h_File(Catalog_h_File,CatalogName,GetString))
  292.             DisplayMsg("Can't Create H Catalog File");
  293.  
  294.         PrintInfo(App,FilePart(Catalog_c_File));
  295.         if (!Write_Catalog_c_File(Catalog_c_File,CatalogName,GetString))
  296.             DisplayMsg("Can't Create C Catalog File");
  297.     }
  298.  
  299.     set(App->TX_Prg_Name,MUIA_Text_Contents,"");
  300.  
  301.     FreeFileNameMemory();
  302.     
  303.     SetDataQuit(data);
  304.     SetFunctionQuit(quit_f);
  305. }
  306.